#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int INF = 1e9 + 9;
int a[MAXN], rmin[MAXN], rmax[MAXN], lmin[MAXN], lmax[MAXN];
// rmin[i]表示第i个数及右边所有的数的最小值，lmax[i]表示第i个数及左边所有数的最大值

int main() {
  int n, k, ans = INF;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  lmin[0] = rmin[n + 1] = INF;
  for (int i = 1; i <= n; i++) {
    lmin[i] = min(lmin[i - 1], a[i]);
    lmax[i] = max(lmax[i - 1], a[i]);
  }
  for (int i = n; i >= 1; i--) {
    rmin[i] = min(rmin[i + 1], a[i]);
    rmax[i] = max(rmax[i + 1], a[i]);
  }

  // 注意从2开始，因为第一个和最后一个一定要算在内
  for (int i = 2; i <= n - k; i++)
    ans =
        min(ans, max(lmax[i - 1], rmax[i + k]) - min(lmin[i - 1], rmin[i + k]));
  cout << ans << endl;
  return 0;
}
